package cn.wk.product.controller;

import cn.wk.basic.utils.AjaxResult;
import cn.wk.basic.utils.PageBean;
import cn.wk.org.domain.Department;
import cn.wk.product.domain.Product;
import cn.wk.product.query.ProductQuery;
import cn.wk.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;


    /**
     * 页面展示加上高级查询
     * @param productQuery
     * @return
     */
    @PostMapping
    public PageBean<Product> queryAll(@RequestBody ProductQuery productQuery){
        System.out.println(productQuery);
        return productService.queryAll(productQuery);
    }

    /**
     * 添加或者修改数据
     * @param product
     * @return
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product){
        try{        //判断id
            if(product.getId() != null){
                productService.update(product);
            }else {
                productService.save(product);
            }
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("错误");
        }
        return new AjaxResult();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){      //删除
        System.out.println(id);
        try{
            productService.delete(id);
            return new AjaxResult();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("del错误");
        }
    }

    @PostMapping("/delOne")
    public AjaxResult del(@RequestBody Department department){      //删除
        System.out.println(department.getId());
        //System.out.println(map.get("id"));
        //从对象中取出id的值
        Long id = department.getId();

        try{
            productService.delete(id);
            return AjaxResult.me();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("del错误");
        }
    }

//    @GetMapping("/{id}")
//    public Department findById(@PathVariable("id") Long id){        //根据id查询
//        return productService.findById(id);
//    }

//    @ResponseBody
//    @GetMapping
//    public List<Department> findAll(){          //查询所有
//        return productService.findAll();
//    }

    /**
     * 批量删除product和productdetail
     * @param ProductS
     * @return
     */
    @PostMapping("/batch")
    public AjaxResult delBatch(@RequestBody List<Product> ProductS){
        System.out.println(ProductS);
        try{
            productService.delBatch(ProductS);
            return new AjaxResult();
        }catch(Exception e){
            return new AjaxResult("操作失败");
        }
    }

    /**
     * 上架
     * @param products
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Product> products){
        try {
            //批量上架产品服务
            productService.onsale(products);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 下架
     * @param products
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Product> products){
        try {
            //批量上架产品服务
            productService.offsale(products);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(e.getMessage());
        }
    }

//------------网站首页---------------------------------------------------------------------------------
    /**
     * 网站首页展示加上高级查询
     * @param productQuery
     * @return
     */
    @PostMapping("/findProducts")
    public PageBean<Product> findProducts(@RequestBody ProductQuery productQuery){
        System.out.println(productQuery);
        return productService.queryAll(productQuery);
    }

    @GetMapping("/{id}")
    public Product queryById(@PathVariable("id") Long id){
        return  productService.findById(id);
    }
}
